/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "逻辑类JIT函数.h"

#include <omp.h>
#include <matXX.h>
#include <graph采样.h>
#include <core/动画/curve.h>
#include <core/几何计算/vec_array.h>


void f_JIT_区间曲线采样(const S_F32Array* 时间, const S_F32Array* 起点, const S_F32Array* 长度, const S_曲线& 曲线, S_F32Array* 采样, const vec2& 钳制) {
	uint32 num = DEF_Max(时间->count, 起点->count);
	num = DEF_Max(num, 长度->count);

	bool 时间多例 = 时间->count >= num;
	bool 起点多例 = 起点->count >= num;
	bool 长度多例 = 长度->count >= num;

	f_core_array_resize((S_Array*)采样, num);

	for (uint32 i = 0; i < num; ++i) {
		float32 t = 时间多例 ? 时间->ptr_userData[i] : 时间->ptr_userData[0];
		float32 s = 起点多例 ? 起点->ptr_userData[i] : 起点->ptr_userData[0];

		t -= s;
		t /= 长度多例 ? 长度->ptr_userData[i] : 长度->ptr_userData[0];

		float32 v = f_graph_钳制曲线采样(t, 曲线);

		采样->ptr_userData[i] = v;
	}
}

void f_JIT_线性轨道采样(const S_F32Array* 时间, const S_F32Array* 起点, const S_F32Array* 长度, int8 起点反向, S_F32Array* 采样) {
	uint32 num = DEF_Max(时间->count, 起点->count);
	num = DEF_Max(num, 长度->count);

	bool 时间多例 = 时间->count >= num;
	bool 起点多例 = 起点->count >= num;
	bool 长度多例 = 长度->count >= num;

	f_core_array_resize((S_Array*)采样, num);

	float32 时间区间 = 0;
	float32 最小起点 = FLT_MAX;
	float32 最大起点 = FLT_MIN;
	if (起点反向) {
		
		
		for (uint32 i = 0; i < num; ++i) {
			float32 s = 起点多例 ? 起点->ptr_userData[i] : 起点->ptr_userData[0];

			if (s > 最大起点) {
				最大起点 = s;
			}
			if (s < 最小起点) {
				最小起点 = s;
			}
		}
		时间区间 = 最大起点 - 最小起点;
	}

	for (uint32 i = 0; i < num; ++i) {
		float32 t = 时间多例 ? 时间->ptr_userData[i] : 时间->ptr_userData[0];
		float32 起点时间 = 起点多例 ? 起点->ptr_userData[i] : 起点->ptr_userData[0];

		if (起点反向) {
			起点时间 = 最小起点 + (最大起点 - 起点时间);
		}

		t -= 起点时间;
		t /= 长度多例 ? 长度->ptr_userData[i] : 长度->ptr_userData[0];

		采样->ptr_userData[i] = clamp(t, 0, 1);
	}
}

void f_node_所有逻辑类节点JIT初始化() {
	f_代码构建_添加JIT初始化函数指针(DEF_S(f_omp_set_num_threads), DEF_S(void), "int32, const S_Props& ", f_omp_set_num_threads);
	f_代码构建_添加JIT初始化函数指针(DEF_S(f_omp_set_threadsNum), DEF_S(void), "int32 ", f_omp_set_threadsNum);

	f_代码构建_添加JIT初始化函数指针(DEF_S(f_JIT_区间曲线采样), DEF_S(void), "const S_F32Array*, const S_F32Array*, const S_F32Array*, const S_曲线&, S_F32Array*, const vec2& ", f_JIT_区间曲线采样);
	f_代码构建_添加JIT初始化函数指针(DEF_S(f_JIT_线性轨道采样), DEF_S(void), "const S_F32Array*, const S_F32Array*, const S_F32Array*, int8 , S_F32Array*", f_JIT_线性轨道采样);

}



